<template>
	<view class="container">
		<!-- start banner -->
		<view class="home_banner">
			<image src="/static/imgs/banner.png" mode="aspectFill"></image>
		</view>
		<!-- end -->
		<!--start 商家端信息 -->
		<view class="home_info" v-if="authType==1">
			<!-- start 已登录-->
			<view class="home_info_header row" v-if="storeId">
				<image :src="storeInfo.store_info.store_pic" mode="aspectFill"></image>
				<view class="home_info_content">
					<view class="store_name">{{storeInfo.store_info.store_name}}</view>
					<view>
						<text>闭店{{storeInfo.store_info.end_time}}</text>
						<switch @change="storeStatusChange" color="#FF9D11" :checked="storeInfo.store_info.status==1" />
						<text>营业{{storeInfo.store_info.start_time}}</text>
					</view>
				</view>
			</view>
			<!-- start 未登录-->
			<view class="home_info_header row" v-else>
				<view class="home_login" @click="toLogin">
					登录
				</view>
			</view>
			<!-- start 时间筛选 -->
			<view class="picker_content">
				<picker :value="startDateValue" mode="date" @change="bindStartDateChange">
					<view class="uni-input">{{startDateValue||'开始时间'}}
						<image src="../../../static/imgs/time.png" class="date_time"></image>
					</view>
				</picker>
				<text class="date_button"></text>
				<picker :value="endDateValue" mode="date" @change="bindEndDateChange">
					<view class="uni-input">{{endDateValue||'截止时间'}}
						<image src="../../../static/imgs/time.png" class="date_time"></image>
					</view>
				</picker>
			</view>
			<!-- end 时间筛选 -->
			<!-- start 收益 -->
			<view class="homecontent_shouyi">
				<view class="homeshouyi">
					<image src="/static/imgs/money.png"></image>
					收益
				</view>
				<text class="home_shouyi_pirce">{{storeInfo.store_get_price||'0.00'}}元</text>
			</view>
			<!-- end -->
		</view>
		<!--end 商家端信息 -->
		<!--start 商家端信息 -->
		<view class="home_info_none" v-else>
			<!-- start 已登录-->
			<view class="home_info_header row" v-if="storeId">
				<image :src="storeInfo.store_info.store_pic" mode="aspectFill"></image>
				<view class="home_info_content row row-center">
					<view class="store_name">{{storeInfo.store_info.store_name}}</view>
				</view>
			</view>
			<!-- start 未登录-->
			<view class="home_info_header row" v-else>
				<view class="home_login" @click="toLogin">
					登录
				</view>
			</view>

		</view>
		<!--end 商家端信息 -->

		<!--start 商户端菜单入口 -->
		<view class="home_menu">
			<view @click="onDiscountsTap" v-if="authType==1">
				<image src="../../../static/imgs/home1.png"></image>
				<text>店家优惠</text>
			</view>
			<view @click="onAddCreateTap" v-if="authType==1">
				<image src="../../../static/imgs/home2.png"></image>
				<text>添加优惠</text>
			</view>
			<view @click="onCardrechargeTap">
				<image src="../../../static/imgs/home3.png"></image>
				<text>会员卡充值</text>
			</view>
			<view @click="onOrderListTap" v-if="authType==1">
				<image src="../../../static/imgs/home4.png"></image>
				<text>店家订单</text>
			</view>
			<view @click="onCanCellationTap">
				<image src="../../../static/imgs/home5.png"></image>
				<text>扫码核销</text>
			</view>
			<view @click="logout" v-if="storeId">
				<image src="../../../static/imgs/home6.png"></image>
				<text>退出登录</text>
			</view>
		</view>
		<!--end 商户端菜单入口 -->
	</view>
</template>

<script>
	var app = getApp();
	import http from '../../../common/js/request.js';
	import common from '../../../common/js/function.js';
	export default {
		data() {
			const currentDate = this.getDate({
				format: true,
			});
			return {
				date: currentDate,
				storeId: uni.getStorageSync('storeId') || null,
				authType: uni.getStorageSync('authType') || 1,
				storeInfo: [],
				startDateValue: '',
				endDateValue: '',
			}
		},
		onShow() {
			var that = this;
			that.storeId = uni.getStorageSync('storeId') || null;
			app.globalData.init(() => {
				that.getStoreInfo();
			});
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			//商家详情
			getStoreInfo() {
				var that = this;
				http.post('api/store/store/Store_info', {
					star_time: that.startDateValue,
					end_time: that.endDateValue,
				}, res => {
					that.storeInfo = res.datas;
				});
			},
			//休息营业切换
			storeStatusChange() {
				var that = this;
				var status = that.storeInfo.store_info.status == 1 ? 2 : 1;
				http.post('api/store/store/store_down_up', {
					status
				}, res => {
					if (res.state == 1) {
						that.storeInfo.store_info.status = status;
					} else {
						uni.showToast({
							title: '操作失败~'
						});
					}
				})
			},
			//改变开始时间
			bindStartDateChange(e) {
				var that = this;
				that.startDateValue = e.detail.value;
				that.getStoreInfo();
			},
			//改变结束时间
			bindEndDateChange(e) {
				var that = this;
				that.endDateValue = e.detail.value;
				that.getStoreInfo();
			},

			//日期选择器必备
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},

			//跳转店家订单
			onOrderListTap() {
				common.verifyAuth(() => {
					uni.navigateTo({
						url: "../order/list/list"
					});
				});

			},
			//会员卡充值
			onCardrechargeTap() {
				common.verifyAuth(() => {
					uni.scanCode({
						success: function(res) {
							uni.showLoading();
							var result = res.result;
							var jsonResult = common.urlFormatToJson(result);
							console.log('条码内容', jsonResult);
							if (!jsonResult.member_id || !jsonResult.store_id) {
								uni.showToast({
									title: '请扫描正确的会员码~',
									icon: 'none',
									success() {
										setTimeout(function() {
											uni.hideLoading();
										}, 500);
									}
								});
								return;
							}
							uni.hideLoading();
							uni.navigateTo({
								url: "../memberCard/recharge/recharge?" + res.result
							});
						}
					});
				});
			},
			//优惠列表
			onDiscountsTap() {
				common.verifyAuth(() => {
					uni.navigateTo({
						url: "../discounts/list/list"
					})
				});
			},
			//添加优惠
			onAddCreateTap() {
				common.verifyAuth(() => {
					uni.navigateTo({
						url: "../discounts/create/create"
					})
				});
			},
			//扫码核销
			onCanCellationTap() {
				common.verifyAuth(() => {
					common.scanCode();
				});
			},
			//退出登录
			logout() {
				var that = this;
				wx.removeStorageSync('storeId');
				uni.navigateTo({
					url: '/pages/merchant/login/login',
				});
			},
			toLogin() {
				uni.navigateTo({
					url: '/pages/merchant/login/login'
				});
			},
			onShareAppMessage(){
				
			}
		}
	}
</script>

<style>
	.store_name {
		font-weight: Medium;
	}

	.row-center {
		align-items: center;
	}

	.picker_content {
		display: flex;
		flex-direction: row;
		width: 580rpx;
		margin: 44rpx auto 0;
	}

	picker {
		width: 226rpx;
		height: 48rpx;
		line-height: 48rpx;
		border: 1px solid #313131;
		border-radius: 10rpx;
		font-size: 28rpx;
		color: #2C2C2C;
		padding: 0 10rpx;
	}

	picker .uni-input {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.date_button {
		width: 53rpx;
		height: 1rpx;
		background: #C2C2C2;
		display: block;
		margin: 28rpx 10rpx 0;
	}

	.date_time {
		width: 26rpx;
		height: 26rpx;
		display: inline-block;
		margin-top: 10rpx;
	}

	.home_info {
		width: 696rpx;
		height: 434rpx;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 2rpx 14rpx 1rpx rgba(210, 210, 210, 1);
		border-radius: 10rpx;
		margin: 20rpx auto 0;
	}

	.home_info_none {
		width: 696rpx;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 2rpx 14rpx 1rpx rgba(210, 210, 210, 1);
		border-radius: 10rpx;
		margin: 20rpx auto 0;
	}

	.home_info_header {
		padding: 26rpx;
		border-bottom: 1px solid #E4E4E4;
	}

	.home_info_header image {
		width: 126rpx;
		height: 126rpx;
		display: block;
	}

	.home_info_content {
		margin-left: 36rpx;
		font-size: 30rpx;
		line-height: 58rpx;

	}

	.home_info_content text {
		font-size: 28rpx;
		color: #2C2C2C;
	}

	switch {
		transform: scale(0.6);
	}

	.homeshouyi {
		text-align: center;
		font-size: 30rpx;

	}

	.homeshouyi image {
		width: 40rpx;
		height: 36rpx;
		margin-right: 7rpx;
	}

	.home_shouyi_pirce {
		color: #FF9D11;
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
		display: block;
	}

	.homecontent_shouyi {
		line-height: 60rpx;
		margin-top: 30rpx;
	}

	.home_banner {
		width: 100%;
		height: 350rpx;
	}

	.home_banner image {
		width: 100%;
		height: 100%;
	}

	.home_menu {
		padding: 0 10rpx;
		margin-top: 20rpx;
	}

	.home_menu>view {
		width: 338rpx;
		height: 242rpx;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0rpx 2rpx 16rpx 0rpx rgba(149, 149, 149, 0.2);
		border-radius: 16rpx;
		float: left;
		margin: 13rpx;
	}

	.home_menu>view image {
		width: 105rpx;
		height: 105rpx;
		display: block;
		margin: 33rpx auto 0;
	}

	.home_menu>view text {
		display: block;
		margin-top: 40rpx;
		font-size: 30rpx;
		font-family: Lantinghei SC;
		font-weight: 600;
		color: rgba(51, 51, 51, 1);
		text-align: center;
	}

	.home_login {
		width: 596rpx;
		height: 98rpx;
		line-height: 98rpx;
		background: rgba(255, 157, 17, 1);
		border-radius: 10rpx;
		color: #fff;
		font-size: 30rpx;
		text-align: center;
		margin: 0 auto;
	}
</style>
